﻿using JustMetClient.Launch.Commons;
using JustMetClient.Launch.Helpers;
using Azylee.Core.AppUtils;
using Azylee.Core.IOUtils.DirUtils;
using Azylee.Core.IOUtils.TxtUtils;
using Azylee.Core.ProcessUtils;
using Azylee.Core.ThreadUtils.SleepUtils;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace JustMetClient.Launch
{
    static class Program
    {
        static AppUnique AppUnique = new AppUnique();

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            //将启动器添加到开机启动
            bool regFlag = StartupTool.Register("JustMetClient.Launch.exe", Application.ExecutablePath, true, false);
            //程序单开验证
            if (AppUnique.IsUnique("JustMetClient.Launch"))
            {
                InstallHelper.NewInstall();//对初装进行处理

                if (args.Contains("-delay5"))
                {
                    Sleep.S(5);
                }
                if (args.Contains("-delay10"))
                {
                    Sleep.S(10);
                }
                if (args.Contains("-delay30"))
                {
                    Sleep.S(30);
                }

                //启动指定版本客户端
                string newVersion = IniTool.GetString(R.Files.Datas, "Version", "JustMetClient-New", R.NoNewVersion);
                string newExe = DirTool.Combine(R.Paths.Root, newVersion, "JustMetClient.exe");
                bool isStart = false;
                if (!string.IsNullOrWhiteSpace(newVersion) && File.Exists(newExe))
                {
                    isStart = ProcessTool.Start(newExe);
                }

                //如果启动失败，则启动最新版本客户端
                if (!isStart) AppLaunchTool.StartNewVersion(R.Paths.Root, R.AppName);
            }

        }

    }
}
